package com.jdk.demo.nio.bytebuffer;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class EqualsDemo {
	
	private ByteBuffer thiz;
	
	// 判断是不是自身，如果是自身，则返回true
	// 判断是不是ByteBuffer的实例，如果不是，则返回false
	// 判断remaining值是否一样，如果不一样，则返回false
	// 判断两个缓冲区中的position与limit之间的数据是否完全一样，只要有一个字节不同，就返回false，否则返回true
	// ByeBiuffer的equals源码
	public boolean equals(Object ob){
		if(thiz == ob){
			return true;
		}
		if(!(ob instanceof ByteBuffer)){
			return false;
		}
		ByteBuffer that = (ByteBuffer)ob;
		if(thiz.remaining() != that.remaining()){
			return false;
		}
		int p = thiz.position();
		for(int i=thiz.limit()-1, j=that.limit()-1; i>=p; i--, j--){
			if(!equals(thiz.get(i), that.get(j))){
				return false;
			}
		}
		return true;
	}

	private boolean equals(byte x, byte y) {
		return x  == y;
	}
	
	public static void main(String[] args) {
		test1();
		test2();
		test3();
		test4();
	}

	// 判断是不是自身，如果是自身，则返回true
	private static void test1() {
		byte[] byteArrayIn1 = {1, 2, 3, 4, 5};
		ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
		System.out.println(bytebuffer1.equals(bytebuffer1));
	}
	
	// 判断是不是ByteBuffer的实例，如果不是，则返回false
	private static void test2() {
		byte[] byteArrayIn1 = {1, 2, 3, 4, 5};
		int[] intArrayIn2 = {1, 2, 3, 4, 5};
		ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
		IntBuffer intbuffer2 = IntBuffer.wrap(intArrayIn2);
		System.out.println(bytebuffer1.equals(intbuffer2));
	}
	
	// 判断remaining值是否一样，如果不一样，则返回false
	private static void test3() {
		byte[] byteArrayIn1 = {3, 4, 5};
		byte[] byteArrayIn2 = {1, 2, 3, 4, 5, 6, 7, 8};
		ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
		ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);
		
		bytebuffer1.position(0);
		bytebuffer2.position(3);
		System.out.println("bytebuffer1 capacity=" + bytebuffer1.capacity() + ", position=" + bytebuffer1.position() + " limit=" + bytebuffer1.limit());
		System.out.println("bytebuffer2 capacity=" + bytebuffer2.capacity() + ", position=" + bytebuffer2.position() + " limit=" + bytebuffer2.limit());
		System.out.println(bytebuffer1.equals(bytebuffer2));
	}
	
	// 判断两个缓冲区中的position与limit之间的数据是否完全一样，只要有一个字节不同，就返回false，否则返回true
	private static void test4() {
		byte[] byteArrayIn1 = {3, 4, 5};
		byte[] byteArrayIn2 = {1, 2, 3, 4, 5, 6, 7, 8};
		ByteBuffer bytebuffer1 = ByteBuffer.wrap(byteArrayIn1);
		ByteBuffer bytebuffer2 = ByteBuffer.wrap(byteArrayIn2);
		
		bytebuffer1.position(0);
		bytebuffer1.limit(3);
		bytebuffer2.position(2);
		bytebuffer2.limit(5);
		System.out.println("bytebuffer1 capacity=" + bytebuffer1.capacity() + ", position=" + bytebuffer1.position() + " limit=" + bytebuffer1.limit());
		System.out.println("bytebuffer2 capacity=" + bytebuffer2.capacity() + ", position=" + bytebuffer2.position() + " limit=" + bytebuffer2.limit());
		System.out.println(bytebuffer1.equals(bytebuffer2));
		
		bytebuffer2.put(3, (byte)44);
		System.out.println(bytebuffer1.equals(bytebuffer2));
	}

}
